#include "xAnimationSetCollect.h"
#include "xHelp.h"
#include "xLogManager.h"

namespace XE	
{
	

	xAnimationSetCollect::xAnimationSetCollect()
		:m_CurrentTime(0.0f)
	{

	}
	xAnimationSetCollect::~xAnimationSetCollect()
	{
      DestoryAllAniamtionSet();
	}

	xAnimationSet* xAnimationSetCollect::CreateAnimationSet(const xString& name)
	{ 
		xAnimationSet* pSet = GetAnimationsetByName(name);
		if(pSet!=NULL)
		{
			xLogMessager::getSingleton().logMessage("create Animationset Fail,has same name:"+ name);
			return NULL;
		}
	
	    pSet =new xAnimationSet(name);
		m_AsetVector.push_back(pSet);
		if(m_ActiveSetVector.empty())
		{
			this->ActiveAnimationSet(name,1.0f);
		}
		return pSet;

	}

	xAnimationSet* xAnimationSetCollect::CreateAnimationSet()
	{
		xString name = "UnNameAnimationSet";
		static int cc = 0;
		name+=xHelp::IntToString(cc++);
		return  CreateAnimationSet(name);
	}


	xAnimationSet* xAnimationSetCollect::GetAnimationsetByName(const xString& name) const 
	{
		size_t size = m_AsetVector.size();
		for(size_t i = 0;i<size;++i)
		{
			if(m_AsetVector[i]->GetName()==name)
				return m_AsetVector	[i];
		}

		return NULL;
	}

	size_t xAnimationSetCollect::GetAnimationSetCount() const
	{
         return m_AsetVector.size();
	}

	size_t xAnimationSetCollect::GetActiveAnimationSetCount() const
	{

		return m_ActiveSetVector.size();
	}


	void xAnimationSetCollect::DestoryAllAniamtionSet()
	{
		size_t size = m_AsetVector.size();
		for(size_t i = 0;i<size;++i)
		{
			SafeDelete(m_AsetVector[i]);
		}
		m_AsetVector.clear();
		m_ActiveSetVector.clear();
	
		return ;

	}


	xAnimationSet* xAnimationSetCollect::GetAnimationSet(UINT index)
	{
      if(index>m_AsetVector.size()-1)
		  return NULL;
	  return m_AsetVector[index];
	}


	void xAnimationSetCollect::Reset()
	{
		m_ActiveSetVector.clear();
        m_CurrentTime = 0.0f;

	}

	void xAnimationSetCollect::AddTime(float time)
	{
		m_CurrentTime+=time;

		if(m_CurrentTime>5000)
			m_CurrentTime = 0.0f;

		xAnimationSetVector::iterator	it = m_ActiveSetVector.begin();
		xAnimationSetVector::iterator end = m_ActiveSetVector.end();
		for(;it!=end;++it)
		{
			(*it)->Addtime(time);
		}

		return;

	}

	bool xAnimationSetCollect::ActiveAnimationSet(const xString& name,float factor)
	{
		xAnimationSet* pAniset =   GetAnimationsetByName(name);
		if(pAniset==NULL)
			return false;
	   xAnimationSetVector::iterator it=  std::find(m_ActiveSetVector.begin(),m_ActiveSetVector.end(),(pAniset));
       if(it!=m_ActiveSetVector.end())
	   {
		   (*it)->SetFactor(factor);
		   return true;
	   }
	   pAniset->SetFactor(factor);
	   m_ActiveSetVector.push_back(pAniset);
	   return true;


	}

	bool xAnimationSetCollect::ActiveAnimationSet(UINT index,float factor)
	{
		if(index>(UINT)m_AsetVector.size()-1)
			return false;
		xAnimationSet* pAniset = m_AsetVector.at(index);
		if(pAniset==NULL)
			return false;
		xAnimationSetVector::iterator it=  std::find(m_ActiveSetVector.begin(),m_ActiveSetVector.end(),(pAniset));
		if(it!=m_ActiveSetVector.end())
		{
			(*it)->SetFactor(factor);
			return true;
		}
		pAniset->SetFactor(factor);
		m_ActiveSetVector.push_back(pAniset);
		return true;

	}

	bool  xAnimationSetCollect::DisActiveAnimaionSet(const xString& name)
	{
		xAnimationSet* pAniset =   GetAnimationsetByName(name);
		if(pAniset==NULL)
			return false;
		xAnimationSetVector::iterator it=   std::find(m_ActiveSetVector.begin(),m_ActiveSetVector.end(),pAniset);
		if(it!=m_ActiveSetVector.end())
		{
			m_ActiveSetVector.erase(it);
			return true ;
		}
		return false;
	}

	bool  xAnimationSetCollect::DisAcitveAllAnimationSet()
	{
          m_ActiveSetVector.clear();
		  return true;
	}

	bool xAnimationSetCollect::IsActiveAnimationSet(const xString& name)const 
	{
		xAnimationSet* pAniset =   GetAnimationsetByName(name);
		if(pAniset==NULL)
			return false;
		xAnimationSetVector::const_iterator it=std::find(m_ActiveSetVector.begin(),m_ActiveSetVector.end(),pAniset);
		if(it!=m_ActiveSetVector.end())
			return true;
		return false;
	}


	bool xAnimationSetCollect::DestoryAnimationSet(const xString& name)
	{
		xAnimationSet* pAset = NULL;
		size_t size = m_AsetVector.size();
		for(size_t i = 0;i<size;++i)
		{
			if(m_AsetVector.at(i)->GetName()==name)
			{
				pAset= m_AsetVector[i];
				m_AsetVector.erase(m_AsetVector.begin()+i);
				break;
			}
		}

		if(pAset!=NULL)
		{
			xAnimationSetVector::iterator it = std::find(m_ActiveSetVector.begin(),m_ActiveSetVector.end(),pAset);
			if(it!=m_ActiveSetVector.end())
			{
				m_ActiveSetVector.erase(it);
			}
		}
		SafeDelete(pAset);
		return true;
	}

}